//
//  PAChartListVC.m
//  PilotSeries
//
//  Created by 黄立强 on 16/7/1.
//  Copyright © 2016年 leon.huang. All rights reserved.
//

#import "PAChartListVC.h"
#import "PARangeListCell.h"
#import "ChartConfig.h"
#import "PAAccountManager.h"
#import "PAChartConfigManager.h"
#import "PAGroupManager.h"
#import "ChartGroup.h"
#import "PACreateDirectionConfigVC.h"

@interface PAChartListVC ()
@property (nonatomic, strong) NSArray * chartConfigs;
@end

@implementation PAChartListVC


#pragma mark - 懒加载
- (NSArray *)chartConfigs
{
    if (_chartConfigs == nil) {
        _chartConfigs = [PAAppContext sharedInstance].chartConfigs;
    }
    return _chartConfigs;
}
static NSString * const PARangeListCell_ID = @"rangelist_cell";
- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置tableView属性
    self.tableView.backgroundColor = PAGlobalBg;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.title = @"ServerCharts";
//    UIBarButtonItem *createChart  =  [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(createDirection:)];
//    // 设置导航栏左边的按钮
//    self.navigationItem.rightBarButtonItems =@[createChart];
    // 注册
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PARangeListCell class]) bundle:nil] forCellReuseIdentifier:PARangeListCell_ID];
}


#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.chartConfigs.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PARangeListCell *cell = [tableView dequeueReusableCellWithIdentifier:PARangeListCell_ID];
    ChartConfig *chartConfig = self.chartConfigs[indexPath.row];
    cell.textLabel.text =chartConfig.chartName;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (![self checkDockSize]) return;
    ChartConfig *chartConfig = self.chartConfigs[indexPath.row];
    BOOL isExist = [PAChartConfigManager isExistInCurrentChart:chartConfig.chartName];
    if (isExist) {
        //已存在
        [CRToastManager showNotificationWithMessage:@"this chart already exists!" completionBlock:^{
          
        }];
        return;
    }
    //1.发送configToserver
//    if(![[PAWebSocketManager sharedInstance] sendMsg:chartConfig.config.mj_JSONString]) return;
    
    //2 .添加到数据库中
    [PAChartConfigManager saveChartConfig:chartConfig];
    
    // 3.查看是否选中群组，如果选中，则添加到群组中
    NSString *groupName = [PAAccountManager loadCurrentGroupName];
    if (groupName!=nil &&![groupName isEqualToString:@""]) {
        
        [PAGroupManager addChartConfig:chartConfig.chartName toGroup:groupName];
    }else{
         [PAGroupManager updateGroup:@"Default"  withChartNames:@[chartConfig.chartName]];
        
    }

    //4.通知collectionview 添加graph
    [[NSNotificationCenter defaultCenter] postNotificationName:PAChartListVCItemClickNotification object:nil userInfo:@{@"chartConfig":chartConfig}];
    //5.跳转到首页，并隐藏
    [self.navigationController popToRootViewControllerAnimated:YES];
    self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
    
}

- (BOOL)checkDockSize
{
    NSInteger size = [PAAccountManager loadCurrentDockSize];
    NSArray *chartConfigs =  [PAChartConfigManager loadCurrentChartConfigs];
    if (size<=chartConfigs.count) {
        [CRToastManager showNotificationWithMessage:@"already more than limit chart size!please change dockSize first!" completionBlock:^{
            
        }];
        return NO;
    }
    return YES;
}
-(void)createDirection:(UIBarButtonItem *)buttonItem {
    PACreateDirectionConfigVC *dv = [[PACreateDirectionConfigVC alloc] init];
    [self.navigationController pushViewController:dv animated:YES];
}

@end
